<?php

class Admin_LoginController extends Zend_Controller_Action {

    public function init() {
        $this->_helper->layout->setLayout("adminlogin");
    }

    public function indexAction() {
        // action body
    }

    public function loginAction() {
        $this->isLoggedIn();
//        $form = new Admin_Form_Changepassword();
//        $this->view->form = $form;
//        $formErrors = array();
//        if ($this->getRequest()->isPost()) {
//            if ($form->isValid($this->getRequest()->getPost())) {
//                $values = $form->getValues();
//                $identity = Zend_Auth::getInstance()->getIdentity();
//                $original = $identity->password;
//                if (md5($values['old']) != $original) {
//                    array_push($formErrors, "Old password does not match.");
//                } else {
//                    $p1 = $values['new1'];
//                    $p2 = $values['new2'];
//                    if ($p1 !== $p2) {
//                        array_push($formErrors, "New password does not matched");
//                    } else {
//                        $db = new Admin_Model_User();
//                        $db->updateUser(array('password' => md5($p1)), $identity->id);
//                        Zend_Auth::getInstance()->clearIdentity();
//                        $this->_redirect("/admin/success");
//                    }
//                }
//            }
//        }
        $this->isLoggedIn();
        $form = new Admin_Form_Login();
        $this->view->form = $form;

        /**
         * - Check for valid input
         * - Authenticate using adapter
         * - Persist user record to session
         * - Redirect to original requirest URL if present
         */
        $valid = false;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $email = $values['email'];
                $password = md5($values['password']);
                $valid = true;
            }
        }

        if ($valid) {
            $authAdapter = $this->getAuthAction();
            $authAdapter->setIdentity($email)
                    ->setCredential($password);
            $auth = Zend_Auth::getInstance();
            $result = $auth->authenticate($authAdapter);
            if ($result->isValid()) {
                $identity = $authAdapter->getResultRowObject();
                $authStorage = $auth->getStorage();
                $authStorage->write($identity);
                $this->_redirect('/admin/login');
            } else {
                $this->view->message = "Username or Password Invalid";
            }
        }
    }

    public function getAuthAction() {
        $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
        $authAdapter->setTableName('user')
                ->setIdentityColumn('email')
                ->setCredentialColumn('password');
        return $authAdapter;
    }

    public function logoutAction() {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('/admin/login');
    }

    public function isLoggedIn() {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect("/admin/home");
        }
    }

    public function changepasswordAction() {
        $this->_helper->layout->setLayout("admin");
//        $this->isLoggedIn();
        $form = new Admin_Form_Changepassword();
        $this->view->form = $form;
        $formErrors = array();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $identity = Zend_Auth::getInstance()->getIdentity();
                $original = $identity->password;
                if (md5($values['old']) != $original) {
                    array_push($formErrors, "Old password does not match.");
                } else {
                    $p1 = $values['new1'];
                    $p2 = $values['new2'];
                    if ($p1 !== $p2) {
                        array_push($formErrors, "New password does not matched");
                    } else {

                        $db = new Admin_Model_User();
                        $db->updateUser(array('password' => md5($p1)), $identity->id);
                        Zend_Auth::getInstance()->clearIdentity();
                        $this->_redirect("/admin/success");
                    }
                }
            }
        }
        if (sizeof($formErrors) > 0) {
            $this->view->formErrors = $formErrors;
        }
    }

    public function successAction() {
        $this->_helper->layout->setLayout("admin");
    }

}

